import 'package:flutter/material.dart';
import 'package:one_key_edit_ip/ip_config.dart';

class AddPage extends StatefulWidget {
  final IpConfig config;

  const AddPage({Key key, this.config}) : super(key: key);

  @override
  _AddPageState createState() => _AddPageState();
}

class _AddPageState extends State<AddPage> {
  IpConfig config;

  @override
  void initState() {
    config = widget.config ?? IpConfig();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("添加IP配置"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Form(
          child: Builder(
            builder: (context) {
              return Column(
                children: [
                  SizedBox(
                    height: 82,
                    child: TextFormField(
                      initialValue: config.name,
                      decoration: InputDecoration(
                        labelText: "名称",
                      ),
                      onSaved: (val) => config.name = val,
                      validator: (val) {
                        if (val?.isEmpty ?? true) {
                          return "请输入名称";
                        }
                        return null;
                      },
                    ),
                  ),
                  SizedBox(
                    height: 82,
                    child: TextFormField(
                      initialValue: config.macName,
                      decoration: InputDecoration(
                        labelText: "网卡名",
                      ),
                      onSaved: (val) => config.macName = val,
                      validator: (val) {
                        if (val?.isEmpty ?? true) {
                          return "请输入网卡名";
                        }
                        return null;
                      },
                    ),
                  ),
                  SizedBox(
                    height: 82,
                    child: TextFormField(
                      initialValue: config.ip,
                      decoration: InputDecoration(
                        labelText: "IP 地址",
                      ),
                      onSaved: (val) => config.ip = val,
                      validator: (val) {
                        if (val?.isEmpty ?? true) {
                          return "请输入IP 地址";
                        }
                        return null;
                      },
                    ),
                  ),
                  SizedBox(
                    height: 82,
                    child: TextFormField(
                      initialValue: config.mask,
                      decoration: InputDecoration(
                        labelText: "子网掩码",
                      ),
                      onSaved: (val) => config.mask = val,
                      validator: (val) {
                        if (val?.isEmpty ?? true) {
                          return "请输入子网掩码";
                        }
                        return null;
                      },
                    ),
                  ),
                  SizedBox(
                    height: 82,
                    child: TextFormField(
                      initialValue: config.gateway,
                      decoration: InputDecoration(
                        labelText: "网关",
                      ),
                      onSaved: (val) => config.gateway = val,
                      validator: (val) {
                        if (val?.isEmpty ?? true) {
                          return "请输入网关";
                        }
                        return null;
                      },
                    ),
                  ),
                  SizedBox(
                    height: 82,
                    child: TextFormField(
                      initialValue: config.defaultDns,
                      decoration: InputDecoration(
                        labelText: "默认 DNS",
                      ),
                      onSaved: (val) => config.defaultDns = val,
                      validator: (val) {
                        if (val?.isEmpty ?? true) {
                          return "请输入默认 DNS";
                        }
                        return null;
                      },
                    ),
                  ),
                  SizedBox(
                    height: 82,
                    child: TextFormField(
                      initialValue: config.spareDns,
                      decoration: InputDecoration(
                        labelText: "备用 DNS",
                      ),
                      onSaved: (val) => config.spareDns = val,
                    ),
                  ),
                  Container(
                    width: double.infinity,
                    height: 36,
                    padding: const EdgeInsets.only(top: 4),
                    child: RaisedButton(
                      color: Theme.of(context).primaryColor,
                      onPressed: () => _add(context),
                      child: Text(
                        null == widget.config ? "添加" : "修改",
                        style: TextStyle(color: Colors.white),
                      ),
                    ),
                  ),
                ],
              );
            },
          ),
        ),
      ),
    );
  }

  _add(BuildContext context) {
    if (Form.of(context).validate()) {
      Form.of(context).save();
      Navigator.of(context).pop(config);
    }
  }
}
